package com.movie.controller;

import com.movie.dto.ApiResponse;
import com.movie.dto.ClearHistoryRequest;
import com.movie.dto.WatchHistoryRequest;
import com.movie.dto.UserWatchHistoryDTO;
import com.movie.service.UserWatchHistoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/history")
@RequiredArgsConstructor
@Tag(name = "用户观看历史管理", description = "用户观看历史的增删改查接口")
public class UserWatchHistoryController {

    private final UserWatchHistoryService userWatchHistoryService;

    @PostMapping
    @Operation(summary = "添加或更新观看记录", description = "添加或更新用户的观看历史记录")
    public ApiResponse<UserWatchHistoryDTO> addOrUpdateWatchHistory(
            @Parameter(description = "观看记录请求") @RequestBody WatchHistoryRequest request) {
        try {
            UserWatchHistoryDTO history = userWatchHistoryService.addOrUpdateWatchHistory(
                    request.getUserId(), request.getMovieId(), request.getWatchTime(), request.getLastPosition());
            return ApiResponse.success("观看记录更新成功", history);
        } catch (RuntimeException e) {
            return ApiResponse.error(e.getMessage());
        }
    }

    @GetMapping
    @Operation(summary = "获取用户观看历史", description = "获取用户的所有观看历史记录")
    public ApiResponse<List<UserWatchHistoryDTO>> getUserWatchHistory(
            @Parameter(description = "用户ID") @RequestParam Long userId) {
        try {
            List<UserWatchHistoryDTO> history = userWatchHistoryService.getUserWatchHistory(userId);
            return ApiResponse.success(history);
        } catch (RuntimeException e) {
            return ApiResponse.error(e.getMessage());
        }
    }

    @GetMapping("/recent")
    @Operation(summary = "获取最近观看历史", description = "获取用户最近的观看历史记录")
    public ApiResponse<List<UserWatchHistoryDTO>> getRecentWatchHistory(
            @Parameter(description = "用户ID") @RequestParam Long userId) {
        try {
            List<UserWatchHistoryDTO> history = userWatchHistoryService.getRecentWatchHistory(userId);
            return ApiResponse.success(history);
        } catch (RuntimeException e) {
            return ApiResponse.error(e.getMessage());
        }
    }

    @DeleteMapping
    @Operation(summary = "删除观看记录", description = "删除用户的观看历史记录")
    public ApiResponse<Void> deleteWatchHistory(
            @RequestBody WatchHistoryRequest request) {
        try {
            if (request.getId() == null) {
                userWatchHistoryService.clearWatchHistory(request.getUserId());
            } else {
                userWatchHistoryService.deleteWatchHistory(request.getId(), request.getUserId());
            }
            return ApiResponse.success("观看记录删除成功", null);
        } catch (RuntimeException e) {
            return ApiResponse.error(e.getMessage());
        }
    }

    @GetMapping("/count")
    @Operation(summary = "统计观看历史数量", description = "统计用户的观看历史记录数量")
    public ApiResponse<Long> countUserWatchHistory(
            @Parameter(description = "用户ID") @RequestParam Long userId) {
        try {
            long count = userWatchHistoryService.countUserWatchHistory(userId);
            return ApiResponse.success(count);
        } catch (RuntimeException e) {
            return ApiResponse.error(e.getMessage());
        }
    }
}